from rest_framework import serializers
from common.models import College, Grade, Student


class StudentSimpleSerializer(serializers.ModelSerializer):
    class Meta:
        model = Student
        fields = ("stu_name", )


class GradeSimpleSerializer(serializers.ModelSerializer):

    class Meta:
        model = Grade
        fields = ("grade_name", )


class GradeSerializer(serializers.ModelSerializer):

    class Meta:
        model = Grade
        fields = "__all__"


class CollegeSerializer(serializers.ModelSerializer):
    class Meta:
        model = College
        fields = "__all__"


class GradeSearchSerializer(serializers.ModelSerializer):
    collage = CollegeSerializer()
    student = serializers.SerializerMethodField()

    @staticmethod
    def get_student(grade):
        queryset = Student.objects.filter(grade=grade.grade_id)
        return StudentSimpleSerializer(queryset, many=True).data

    class Meta:
        model = Grade
        fields = ("grade_name", "collage", "student")


class CollegeSearchSerializer(serializers.ModelSerializer):
    grade = serializers.SerializerMethodField()
    student = serializers.SerializerMethodField()

    @staticmethod
    def get_grade(college):
        queryset = Grade.objects.filter(collage=college.coll_id)
        return GradeSimpleSerializer(queryset, many=True).data

    @staticmethod
    def get_student(college):
        queryset = Student.objects.filter(collage=college.coll_id)
        return StudentSimpleSerializer(queryset, many=True).data

    class Meta:
        model = College
        fields = ("coll_id", "coll_name", "grade", "student")
